<template>
    <div style="width: 100%;height: 100vh;overflow: hidden;background-color: #8e90ff">
        <div style="width: 300px;margin: 100px auto;text-align: center">
            <div style="color: #fdfdff;font-size: 30px;padding: 30px 0">欢迎登录</div>
            <el-form ref="form" :model="form" :rules="rules" label-width="normal">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.username"
                              style="background-color: #ffffff" placeholder="请输入账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input  style="background-color: #ffffff" prefix-icon="el-icon-lock" v-model="form.password" show-password placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <div style="display: flex">
                        <el-input prefix-icon="el-icon-key" v-model="form.validCode" style="width: 50%;" placeholder="请输入验证码"></el-input>
                        <ValidCode @input="createValidCode" />
                    </div>
                </el-form-item>
                <el-button style="width: 100% ;margin-bottom: 20px;" @click="login" >登录</el-button>
                <a  @click="register" style="color: #FFF;">没有账号，注册一个？</a><br><br>
<!--                <a  href="/" style="color: #ffffff;">去首页看看？</a>-->
            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";

    import ValidCode from "@/components/ValidCode";
    export default {
        name: "Login",
        components: {
            ValidCode
        },
        data() {
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                },
                validCode: '',
                vedioCanPlay:false
            }
        },
        methods: {
            canplay() {
                this.vedioCanPlay = true
            },
            // 接收验证码组件提交的 4位验证码
            createValidCode(data) {
                this.validCode = data
            },
            login() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        if (!this.form.validCode) {
                            this.$message.error("请填写验证码")
                            return
                        }
                        if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
                            this.$message.error("验证码错误")
                            return
                        }
                        request.post("user/login", this.form).then(res => {
                            console.log(res.data)
                            if (res.code === '0') {
                                localStorage.setItem("token",res.data.token)
                                localStorage.setItem("avatar",res.data.avatar)
                                this.$message({
                                    type: "success",
                                    message: "登录成功！"
                                })
                                sessionStorage.setItem("user", JSON.stringify(res.data))
                                this.$router.push("/index")
                            } else {
                                this.$message({
                                    type: "error",
                                    message: res.msg
                                })
                            }
                        })
                    }
                })

            },
            register() {
               let path = this.$router.resolve("register")
                window.open(path.href, '_blank')
            }

        }
    }
</script>

<style scoped>

</style>